﻿Ext.namespace("Swfu");
Ext.namespace("Swfu.BackMgr");

function EditQuestion(id) {
    var DataPannel = new Swfu.BackMgr.QuestionPanel({ IdNo: id });
    var win = new Ext.Window({id: 'QuestionWin', draggable: false, modal: true, resizable: false,
                        title: '试题维护', items: DataPannel
                    });
    win.show();
}
function DelQuestion(id) {
    var url = "/Exam.mvc/Delete";
    Ext.Msg.show({
        title: '系统确认',
        msg: '你是否确定删除这些数据?',
        buttons: Ext.Msg.YESNO,
        scope: this,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: url, //调用谁?
                    success: function (response, options) {
                        var responseMessage = Ext.util.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            Ext.getCmp("QuestionMgr").getStore().reload(); //刷新谁?
                            Ext.ux.Toast.msg('提示', '已经成功删除选中的信息。');
                        }
                        else {
                            Ext.ux.Toast.msg('提示', '删除失败，请您重试。');
                        }
                    },
                    failure: function (action) {
                        if (action.failureType == 'server') {
                            obj = Ext.util.JSON.decode(action.response.responseText);
                            Ext.Msg.alert('错误:', obj.errors.reason);
                        }
                    },params: {Id: id}
                });
            }
        },
        icon: Ext.MessageBox.QUESTION
    });
}

Swfu.BackMgr.QuestionMgr = Ext.extend(Ext.grid.GridPanel, {
    initComponent: function () {
        var sm = new Ext.grid.CheckboxSelectionModel({ singleSelect: true });
        var fm = Ext.form;

        var cm = new Ext.grid.ColumnModel([
            {
                header: "问题描述", width:500,
                dataIndex: 'Question'
            }, {
                header: "题型", width:120,
                dataIndex: 'QuestionType'
            }, {
                header: "答案", width:60,
                dataIndex: 'Answer'
            }, { header: "操作", dataIndex: 'Id', width: 130, renderer: function (value) {
                    var links = [];
                    links.push('<a href="#" onclick="return EditQuestion(' + value + ');">编辑</a>');
                    links.push('<a href="#" onclick="return DelQuestion(' + value + ');">删除</a>');
                    return links.join("&nbsp;");
                }
            }
		]);
        var reader = new Ext.data.JsonReader({
            totalProperty: 'totalCount', //分页使用
            root: 'data',
            autoLoad: true,
            fields: [{
                name: 'Id'
            }, {
                name: 'QuestionType'
            }, {
                name: 'Question'
            }, {
                name: 'Answer'
            }]
        });
        var ds = new Ext.data.Store({
            scopte: this,
            id: 'QuestionDS',
            url: '/Exam.mvc/Find',
            reader: reader
        });
        /*ds.on('beforeload', function (thiz, options) {
        Ext.apply(thiz.baseParams, { idStr: Ext.getCmp('treeDeptMeet').getValue() });
        });*/

        Ext.apply(this, {
            id: 'QuestionMgr',
            plain: true,
            layout: 'fit',
            border: false,
            cm: cm,
            sm: sm,
            frame: true,
            store: ds,
            loadMask: true,
            clicksToEdit: 1,
            tbar: [{
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    var DataPannel = new Swfu.BackMgr.QuestionPanel();
                    var win = new Ext.Window(
                    {
                        id: 'QuestionWin', draggable: false, modal: true, resizable: false,
                        title: '试题维护', items: DataPannel
                    });
                    win.show();
                    Ext.getCmp('BtnSave').on('click', function () { fnSubmitQuestion(1, 0); });
                    //Ext.getCmp('BtnSubmit').on('click', function () { fnSubmitMeeting(1, 1); });
                }
            }, '-', {
                text: "重新加载数据",
                iconCls: 'icon-redo',
                handler: function () {
                    ds.reload();
                }
            }, '->', {
                xtype: 'tbtext',
                text: '部门:'
            }, '-', {
                text: "查询",
                iconCls: 'icon-search',
                handler: function () {
                    ds.reload();
                }
            }],

            //分页
            bbar: new Ext.PagingToolbar({
                pageSize: 20,
                store: ds,
                displayInfo: true,
                displayMsg: '显示第 {0} 条到 {1} 条记录，共 {2} 条',
                emptyMsg: '无记录',
                hidden: false
            }),
            loadMask: true
        });
        Swfu.BackMgr.QuestionMgr.superclass.initComponent.apply(this, arguments);
        ds.load({params: {start: 0,limit: 20}});
    }
});

Ext.reg('QuestionMgr', Swfu.BackMgr.QuestionMgr);